package com.xxc.hs.funActivity.panel;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class ExpertDao {
    private EelFaqDatabase dbHelper;

    public ExpertDao(Context context) {
        dbHelper = new EelFaqDatabase(context);
    }

    // 根据ID获取专家
    public Expert getExpertById(int id) {
        Expert expert = null;
        SQLiteDatabase db = dbHelper.getReadableDatabase();

        String[] projection = {
                EelFaqDatabase.COLUMN_EXPERT_ID,
                EelFaqDatabase.COLUMN_EXPERT_NAME,
                EelFaqDatabase.COLUMN_EXPERT_TITLE,
                EelFaqDatabase.COLUMN_EXPERT_RATING,
                EelFaqDatabase.COLUMN_EXPERT_CONSULT_COUNT,
                EelFaqDatabase.COLUMN_EXPERT_SKILLS
        };

        String selection = EelFaqDatabase.COLUMN_EXPERT_ID + " = ?";
        String[] selectionArgs = {String.valueOf(id)};

        Cursor cursor = db.query(
                EelFaqDatabase.TABLE_EXPERTS,
                projection,
                selection,
                selectionArgs,
                null,
                null,
                null
        );

        if (cursor != null && cursor.moveToFirst()) {
            int expertId = cursor.getInt(cursor.getColumnIndexOrThrow(EelFaqDatabase.COLUMN_EXPERT_ID));
            String name = cursor.getString(cursor.getColumnIndexOrThrow(EelFaqDatabase.COLUMN_EXPERT_NAME));
            String title = cursor.getString(cursor.getColumnIndexOrThrow(EelFaqDatabase.COLUMN_EXPERT_TITLE));
            float rating = cursor.getFloat(cursor.getColumnIndexOrThrow(EelFaqDatabase.COLUMN_EXPERT_RATING));
            int consultCount = cursor.getInt(cursor.getColumnIndexOrThrow(EelFaqDatabase.COLUMN_EXPERT_CONSULT_COUNT));
            String skills = cursor.getString(cursor.getColumnIndexOrThrow(EelFaqDatabase.COLUMN_EXPERT_SKILLS));

            expert = new Expert(expertId, name, title, rating, consultCount, skills);
            cursor.close();
        }

        db.close();
        return expert;
    }
}